//
//  CSMyInfoViewController.m
//  ZSYH
//
//  Created by 主事丫环 on 2019/3/14.
//  Copyright © 2019 魏堰青. All rights reserved.
//

#import "CSMyInfoViewController.h"
#import "CSListDetaileTableViewCell.h"
#import "CSniChengViewController.h"
#import "CSPersonModel.h"
#import "CSNewChangePhoneViewController.h"
#import "MOFSPickerManager.h"

#import "IDcardinfofViewController.h"
#import "BVIPinfomiationViewController.h"
#import "AUserAgreementViewController.h"

#import "ZMyAddressVC.h"//我的地址
#import "BBussinessLicenseVC.h"//上传营业执照
#import "BBussinessLicenseAuditVC.h"//营业执照审核
#import "BBussinessLicenseAuditFailVC.h"//营业执照审核失败
#import "BBussinessLicenseShowVC.h"//营业执照审核通过
#import "BActivateAdministratorVC.h"//激活管理员信息

@interface CSMyInfoViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *mydateSource;
@property(nonatomic,strong)NSArray *titArr;
@property(nonatomic,strong)UIImage *headerImage;
@property(nonatomic,strong)NSString *perston;
@property(nonatomic,strong)NSString *isPower;

@end

@implementation CSMyInfoViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (_myTableView) {
        [self request];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithHexString:@"#DDDBD7"];
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.title = @"个人信息";
    NSString *set = [USER_DEFAULT objectForKey:@"level"];

    self.perston =set;
    
    self.mydateSource = [NSMutableArray arrayWithCapacity:1];
    
    [self request];
}

-(void)request{
    kWeakSelf;
    if (!_model) {
        _model = [[CSPersonModel alloc]init];
    }
    [NetWorkTool POST:CcentenInfo param:nil success:^(id dic) {
    
        [weakSelf.model setValuesForKeysWithDictionary:[dic objectForKey:@"data"]];
        [weakSelf.myTableView reloadData];

    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CSListDetaileTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CSListDetaileTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.leftLB.text = self.titArr[indexPath.row];
    cell.userImage.hidden = YES;
    cell.rightLB.hidden = YES;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //昵称
    if (indexPath.row == 0) {
        cell.rightLB.text = _model.name;
        cell.rightLB.hidden = NO;
    }

    //头像
    if (indexPath.row == 1) {
        cell.userImage.hidden = NO;
        
        if (_model.image_url.length <= 0) {
            cell.userImage.image = [UIImage imageNamed:@"headerIMage"];
        } else {
            if ([_model.image_url hasPrefix:@"http"]) {
                
                [cell.userImage sd_setImageWithURL:[NSURL URLWithString:_model.image_url] placeholderImage:[UIImage imageNamed:@"headerIMage"]];
            } else {
                sdimg(cell.userImage, _model.image_url);
            }
        }
     
        cell.userImage.userInteractionEnabled = YES;
        UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(seleImage:)];
        [cell.userImage addGestureRecognizer:ges];
    }
    
    //手机号
    if (indexPath.row == 2) {
        cell.rightLB.text = _model.phone;
        cell.rightLB.hidden = NO;
    }
    
    if (indexPath.row == 5) {
        if ([_perston integerValue] != 1) {
            //身份认证(市县级)
            cell.rightLB.hidden = NO;
            if ([_model.is_power isEqualToString:@"1"]) {
                cell.rightLB.text= [NSString stringWithFormat:@"管理员(有效期至%@)",[_model.end_time substringToIndex:10]];
            }
            //去激活
            else
            {
                cell.rightLB.text = @"管理员(去激活)";
            }
        }
    }
    return cell;
}

-(void)seleImage:(UITapGestureRecognizer *)sender{
    UIImagePickerController *pick = [[UIImagePickerController alloc]init];
    pick.allowsEditing = YES;
    pick.delegate = self;
    [UIAlertController showPhotoAlerControllerPicker:pick controller:self sourceView:nil];
}

- (void)upLoadHeadIcon:(UIImage *)image {
    self.headerImage = image;
    [MBProgressHUD showHUDAddedTo:self.view msg:@"上传头像..." animated:YES];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //param[@"type"] = @"logo";
    
    [NetWorkTool UploadPicWithUrl:changeInfo param:param key:@"images" image:image withSuccess:^(id dic) {
        [self request];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *image = info[UIImagePickerControllerEditedImage];
    
    [self upLoadHeadIcon:image];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0){
        //昵称
        CSniChengViewController *cell = [[CSniChengViewController alloc]init];
        cell.model = self.model;
        [self.navigationController pushViewController:cell animated:YES];
    }else if (indexPath.row == 1){
        //头像
    }else if (indexPath.row == 2){
        //修改手机号
        CSNewChangePhoneViewController *cell = [[CSNewChangePhoneViewController alloc]init];
        [self.navigationController pushViewController:cell animated:YES];
    }else if (indexPath.row == 3){
        if ([_perston integerValue] == 1) {
            //身份证信息
            IDcardinfofViewController *vc = [IDcardinfofViewController new];
            vc.model = self.model;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            //我的地址
            ZMyAddressVC *vc = [ZMyAddressVC new];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else if (indexPath.row == 4){
        if ([_perston integerValue] == 1) {
            //收货地址
            [self.navigationController pushViewController:[ZMyAddressVC new] animated:YES];
        }else{
//          //营业执照
//            if ([self.model.licenseState integerValue] == 2){
//                //审核通过(查看营业执照)
//                BBussinessLicenseShowVC *vc = [BBussinessLicenseShowVC new];
//                vc.model = self.model;
//                [self.navigationController pushViewController:vc animated:YES];
//            }else{
//                [self requestUserInfo];
//            }
            //身份证信息
            IDcardinfofViewController *idvc = [[IDcardinfofViewController alloc]init];
            idvc.model = self.model;
            [self.navigationController pushViewController:idvc animated:YES];
        }
    } else if (indexPath.row == 5) {
        if ([_perston integerValue] == 1) {
            //用户协议
            AUserAgreementViewController *agreement = [[AUserAgreementViewController alloc] init];
            agreement.model = self.model;
            [self.navigationController pushViewController:agreement animated:YES];
        } else {
            if ([_model.is_power isEqualToString:@"1"]) {
                //身份认证 //已经缴费
                [self.navigationController pushViewController:[BVIPinfomiationViewController new] animated:YES];
                KMyLog(@"是点击的这里 跳转到 入会信息页面的吗");
            } else {
                //身份认证 //暂未缴费
                BActivateAdministratorVC *vc = [[BActivateAdministratorVC alloc] init];
                vc.model = self.model;
                [self.navigationController pushViewController:vc animated:YES];
                KMyLog(@"是点击的这里 跳转到 激活管理员身份页面的吗");
            }
        }
    } else if (indexPath.row == 6){
        if ([_perston integerValue] == 1) {
            //联系我们
            [HFTools callMobilePhone:@"400-961-5811"];
        }else{
            //用户协议
            AUserAgreementViewController *agreement = [[AUserAgreementViewController alloc] init];
            agreement.model = self.model;
            [self.navigationController pushViewController:agreement animated:YES];
        }
    }else if (indexPath.row == 7){
        //联系我们
        [HFTools callMobilePhone:@"400-961-5811"];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

-(UITableView *)myTableView{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kNaviHeight, KWIDTH, KHEIGHT-kNaviHeight) style:(UITableViewStylePlain)];
        _myTableView.backgroundColor = [UIColor clearColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.estimatedRowHeight = 90;
        _myTableView.rowHeight = UITableViewAutomaticDimension;
        _myTableView.tableFooterView = [UIView new];
        [_myTableView registerNib:[UINib nibWithNibName:@"CSListDetaileTableViewCell" bundle:nil] forCellReuseIdentifier:@"CSListDetaileTableViewCell"];
        adjustInset(_myTableView);
        [self.view addSubview:_myTableView];
        
        UIView *footview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 48)];
        footview.backgroundColor = [UIColor whiteColor];
        UILabel*titLB = [[UILabel alloc]initWithFrame:CGRectMake(16, 0, 100, 48)];
        titLB.font = FontSize(14);
        titLB.text = @"退出登录";
        titLB.textColor = [UIColor colorWithHexString:@"#F2614E"];
        [footview addSubview:titLB];
        UIButton *but= [UIButton buttonWithType:(UIButtonTypeCustom)];
        but.frame =footview.bounds;
        [footview addSubview:but];
        [but addTarget:self action:@selector(tuichudnegl:) forControlEvents:(UIControlEventTouchUpInside)];
        _myTableView.tableFooterView = footview;
    }
    return _myTableView;
}

-(void)tuichudnegl:(UIButton *)but{
//    [USER_DEFAULT removeObjectForKey:@"Token"];
//    
//    ShowToastWithText(@"退出成功");
//    [self.navigationController popViewControllerAnimated:YES];
    //退出登录
    [NetWorkTool POST:tcagentLoginOut param:nil success:^(id dic) {
        [USER_DEFAULT removeObjectForKey:@"Token"];
        
        ShowToastWithText(@"退出成功");
        //设置Alias
        [JPUSHService setAlias:nil completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            
        } seq:0];
        
        [self.navigationController popViewControllerAnimated:YES];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

-(void)requestUserInfo{
    kWeakSelf;
    [NetWorkTool POST:CcentenInfo param:nil success:^(id dic) {
        weakSelf.model = [CSPersonModel mj_objectWithKeyValues:[dic objectForKey:@"data"]];
        [weakSelf handleUserInfoModel];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

- (void)handleUserInfoModel{
    if ([self.model.licenseState integerValue] == -1) {
        //拒绝
        BBussinessLicenseAuditFailVC *vc = [BBussinessLicenseAuditFailVC new];
        vc.auditFailReason = self.model.licenseRefuseReason;
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([self.model.licenseState integerValue] == 0){
        //未上传
        [self.navigationController pushViewController:[BBussinessLicenseVC new] animated:YES];
    }else if ([self.model.licenseState integerValue] == 1){
        //审核中
        BBussinessLicenseAuditVC *vc = [BBussinessLicenseAuditVC new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([self.model.licenseState integerValue] == 2){
        //审核通过
        BBussinessLicenseShowVC *vc = [BBussinessLicenseShowVC new];
        vc.model = self.model;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(NSArray *)titArr{
    if (!_titArr) {
        if ([_perston integerValue] == 1) {
            //A级
            _titArr = @[@"昵称",@"头像",@"修改绑定手机号",@"身份证信息",@"收货地址",@"用户协议",@"联系我们"];
        }else{
            //B级有收益
            //_titArr = @[@"昵称",@"头像",@"修改绑定手机号",@"我的地址",@"营业执照",@"身份证信息",@"身份认证",@"用户协议",@"联系我们"];
            _titArr = @[@"昵称",@"头像",@"修改绑定手机号",@"我的地址",@"身份证信息",@"身份认证",@"用户协议",@"联系我们"];
        }
    }
    return _titArr;
}

@end
